﻿/// <reference path="jquery-1.7.1.min.js" />

/// <reference path="jquery-ui-1.8.16.min.js" />


var myLayout;
var hotKey = function (e) {
    e = e || event;
    //console.log(e)
    if (e.keyCode == 120) {
        //console.log("F9")
        $("#processMsg").click()
    }
    if (e.keyCode == 114) {
        $("#searchDiv").click()
        e.stopPropagation()
    }
}
$(document).ready(function () {
    document.onkeydown = hotKey;
    $("input[datatype='datetimepicker']").live("focus", function () {
        $(this).datetimepicker({ dateFormat: "yy-mm-dd", showButtonPanel: true, changeYear: true, showOtherMonths: true, selectOtherMonths: true });
    })
    $("input[datatype='datePicker']").live("focus", function () {
        $(this).datepicker({ dateFormat: "yy-mm-dd", showButtonPanel: true, changeYear: true, showOtherMonths: true, selectOtherMonths: true });
    })
    $("input:submit, a, button", ".demo").button();
    //生成布局
    myLayout = $('body').layout({ applyDefaultStyles: false, east__initClosed: true });
    //myLayout.close("south");
    //myLayout.close("east");
    myLayout.sizePane("north",85);
    myLayout.sizePane("west", 180);
    myLayout.sizePane("south", 20);
    //解决zTree鼠标拖拽的时候不显示拖拽标记
    $("#main").css("z-index", 0)
    //解决zTree鼠标拖拽的时候不显示拖拽标记

    //    $("#main").ajaxStart(function () { OnBegin() });
    $("#main").ajaxStart(function () {$("#loading").show()});
//    $("#main").ajaxSuccess(function (data, status, request) { OnSuccess(data, status, request) });
//    $("#main").ajaxError(function (request, status, error) { OnFailure(request, status, error) });
    //    $("#main").ajaxComplete(function (request, status) { OnComplete(request, status) });
    $("#main").ajaxComplete(function (request, status) { $("#loading").hide() });

    $("#sysInfo").find(".close").live("click", function () {
        $("#sysInfoContent").slideUp(600, function () {
            $(".viewContent").show();
        })
        closeTooltip()
    })
});

function OnBegin() {
    //closeTooltip()
    $("#w-loading div").css("background", "#4FA835")
    $("#w-loading div").stop(true, false); //停止之前的动画
    var width = $("#w-loading div").parents("div").width();
    $("#w-loading div").animate({ width: width * 0.7 }, 10000, "", function () {
        $("#w-loading div").animate({ width: width * 0.9 }, 30000);
    });
    $("#w-loading div").html("")
    //console.log("OnBegin")
}
function OnFailure(request, status, error) {
    if (request.status == undefined) {
        request = status;
    }
    var errMsg
    if (request.status == 0) {
        errMsg = "Connect to server failed";
    }
    else {
        errMsg = request.status + ":" + request.statusText
    }
    completeProgressBar(function () {
        $("#w-loading div").html("<span id='processMsg'> " + errMsg + "</span>")
    });
    showErrorMsg(errMsg + "&nbsp; &nbsp;<a href='javascript:void(0)' onclick=$('#sysInfoContent').slideToggle('slow');$('.viewContent').slideUp('slow')>Details</a>", request.responseText);
    //console.log("OnFailure")
}
function OnSuccess(data, status, request) {
    completeProgressBar(function () {
        $("#w-loading div").html("<span>Complete</span>")
    });
    //console.log("OnSuccess")
}
function OnComplete(request, status) {
    ///关闭右侧菜单
    myLayout.close("east");
    ///关闭右侧菜单
    //console.log("OnComplete")
    //美化table
    $(".db").children("tbody").children().mousemove(function () {
        $(this).addClass("mouseover")
    }).mouseout(function () {
        $(this).removeClass("mouseover")
    }).each(function (i) {
        if (i % 2 == 0) {
            $(this).addClass("row0")
        }
        else {
            $(this).addClass("row1")
        }
    })
    //美化table
}

function closeTooltip() {
    $("#sysInfo").slideUp(600, function () {
        $("#sysInfo").find(".close").prev().html("")
        $("#sysInfo").find(".close").parent().removeAttr("class");
    })
}

function completeProgressBar(callback) {
    $("#w-loading div").stop(true, false); //停止之前的动画
    $("#w-loading div").stop(false, false); //停止之前的动画
    var width = $("#w-loading div").parents("div").width();
    $("#w-loading div").animate({ width: width }, 200, function () {
        $("#w-loading div").fadeOut(200, function () {
            $("#w-loading div").attr("style", "").show();
            var complete = $("#w-loading div span").html()
            if (complete == null || complete.endWith("%") || complete.endWith("0%")) {
                $("#w-loading div").css("background", "#fff")
            }
            setTimeout(function () { callback(); }, 100)
            //callback();
        });
    });
}

String.format = function () {
    if (arguments.length == 0)
        return null;

    var str = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
        var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
        str = str.replace(re, arguments[i]);
    }
    return str;
}
String.prototype.beginWith = function (oString) {
    var reg = new RegExp("^" + oString);
    return reg.test(this);
}
String.prototype.endWith = function (oString) {
    var reg = new RegExp(oString + "$");
    return reg.test(this);
}

function showClientMsg(msgType, msg, msgContent) {
    switch (msgType) {
        case "Error":
            showErrorMsg(msg, msgContent);
            break;
        case "Info":
            showInfoMsg(msg, msgContent);
            break;
        case "Warning":
            showWarningMsg(msg, msgContent);
            break;
        default:
            break;
    }
}

function showErrorMsg(msg, msgContent) {
    showInfoHtml();
    $("#sysInfoTitle").attr("class", "Error").children("p").html(msg);
    $("#sysInfoContent").html(msgContent)
    $("#sysInfo").slideDown("slow", function () {
        //setTimeout(closeTooltip, 3000)
    });
}
function showInfoMsg(msg, msgContent) {
    showInfoHtml();
    $("#sysInfoTitle").attr("class", "Info").children("p").html(msg);
    $("#sysInfoContent").html(msgContent)
    $("#sysInfo").slideDown("slow", function () {
        setTimeout(closeTooltip, 3000)
    });
}
function showWarningMsg(msg, msgContent) {
    showInfoHtml();
    $("#sysInfoTitle").attr("class", "Warning").children("p").html(msg);
    $("#sysInfoContent").html(msgContent)
    $("#sysInfo").slideDown("slow", function () {
        setTimeout(closeTooltip, 3000)
    });
}

function showInfoHtml() {

    if ($("#sysInfo").length == 0) {
        if ($("#currentLocation").length != 0) {
            $("#currentLocation").after("<div id='sysInfo'><div class='sysInfoTitle' id='sysInfoTitle'><p></p><span class='close'>关闭</span></div><div class='sysInfoContent' id='sysInfoContent' style='display:none'></div></div>")
        }
        else {
            $("#main").html("<div id='sysInfo'><div class='sysInfoTitle' id='sysInfoTitle'><p></p><span class='close'>关闭</span></div><div class='sysInfoContent' id='sysInfoContent' style='display:none'></div></div>")
        }
    }
    else {
        $("#sysInfoContent").hide();
    }
}